$(function () {
    $("#jqGrid").jqGrid({
        url: '../customcontract/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden: true},
            { label: '协议编号', name: 'contractno', index: 'contractNO', width: 80 },
			{ label: '单位编码', name: 'unitId', index: 'unit_id', width: 80 },
			{ label: '单位名称', name: 'unitName', index: 'unit_name', width: 80 }, 			
			{ label: '协议开始时间', name: 'contractBgdate', index: 'contract_bgdate', width: 80,formatter:"date",formatoptions:{newformat:'Y-m-d'} },
			{ label: '协议结束时间', name: 'contractEddate', index: 'contract_eddate', width: 80,formatter:"date",formatoptions:{newformat:'Y-m-d'} },
			{ label: '业务描述', name: 'businessdesc', index: 'businessDesc', width: 80 },
			{ label: '合同类型', name: 'cooperationtype', index: 'cooperationType', width: 80 },
			{ label: '责任部门', name: 'responsibilitydepartment', index: 'responsibilityDepartment', width: 80,hidden: true },
			{ label: '责任人', name: 'responsibilityperson', index: 'responsibilityPerson', width: 80,hidden: true },
			{ label: '总金额', name: 'totalje', index: 'totalJE', width: 80 }, 			
			{ label: '合作单位', name: 'cooperationunit', index: 'cooperationUnit', width: 80 }, 			
			{ label: '管理费方式', name: 'managementcosttype', index: 'managementCostType', width: 80 }, 			
			{ label: '协议价格', name: 'agreementprice', index: 'agreementPrice', width: 80,hidden: true },
			{ label: '税率', name: 'taxrate', index: 'taxRate', width: 80,hidden: true },
			{ label: '比例或金额', name: 'percentormoney', index: 'percentOrMoney', width: 80,hidden: true },
			{ label: '其他说明', name: 'remark', index: 'remark', width: 80,hidden: true },
			{ label: '是否有效', name: 'available', index: 'available', width: 80,hidden: true },
			{ label: '经济补偿承担方式', name: 'jjbccdfs', index: 'jjbccdfs', width: 80,hidden: true },
			{ label: '经济补偿承担比例或金额', name: 'jjbccdblhje', index: 'jjbccdblhje', width:80 ,hidden: true },
			{ label: '工伤承担方式', name: 'gscdfs', index: 'gscdfs', width: 80,hidden: true },
			{ label: '工伤承担比例或金额', name: 'gscdblhje', index: 'gscdblhje', width: 80,hidden: true },
			{ label: '工伤', name: 'gongshang', index: 'gongshang', width: 80,hidden: true },
			{ label: '养老', name: 'yanglao', index: 'yanglao', width: 80,hidden: true },
			{ label: '失业', name: 'shiye', index: 'shiye', width: 80,hidden: true},
			{ label: '医疗', name: 'yiliao', index: 'yiliao', width: 80,hidden: true },
			{ label: '生育', name: 'shengyu', index: 'shengyu', width: 80 ,hidden: true},
			{ label: '公积金', name: 'gongjijin', index: 'gongjijin', width: 80,hidden: true }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            unitName: null,
            conForm:null,
            cooperUnit:null
        },
		showList: true,
		title: null,
		customContract: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.customContract = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.customContract.id == null ? "../customcontract/save" : "../customcontract/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.customContract),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../customcontract/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../customcontract/info/"+id, function(r){
                vm.customContract = r.customContract;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'unit_name': vm.q.unitName,
						  'conForm':vm.q.conForm,
					      'cooperUnit':vm.q.cooperUnit},
                page:page
            }).trigger("reloadGrid");
		}
	}
});